package org.jeecg.modules.flow.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.rabbitmq.client.RabbitMqClient;
import org.jeecg.common.base.BaseMap;
import org.jeecg.modules.flow.common.constant.FlowCloudConstant;
import org.jeecg.modules.flow.service.FlowEventMsgService;
import org.jeecg.modules.flow.service.IFlowBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class FlowEventMsgServiceImpl implements FlowEventMsgService {

    @Autowired
    private RabbitMqClient rabbitMqClient;

    @Autowired
    private IFlowBaseService flowBaseService;

    @Override
    public void sendProcessCompletedMsg(String processInstanceId, String businessKey) {
        BaseMap map = new BaseMap();
        map.put(FlowCloudConstant.FLOW_PROCESS_BUSINESS_KEY, businessKey);
        map.put(FlowCloudConstant.FLOW_PROCESS_INSTANCE_ID, processInstanceId);
        rabbitMqClient.sendMessage(FlowCloudConstant.MQ_FLOW_PROCESS_COMPLETED_EVENT, map);
        log.info("流程结束事件消息发送成功,流程实例ID：{}，BusinessKey：{}", processInstanceId, businessKey);
    }

    @Override
    public void sendProcessCancelledMsg(String processInstanceId, String businessKey) {
        BaseMap map = new BaseMap();
        map.put(FlowCloudConstant.FLOW_PROCESS_BUSINESS_KEY, businessKey);
        map.put(FlowCloudConstant.FLOW_PROCESS_INSTANCE_ID, processInstanceId);
        rabbitMqClient.sendMessage(FlowCloudConstant.MQ_FLOW_PROCESS_CANCELLED_EVENT, map);
        log.info("流程终结事件消息发送成功,流程实例ID：{}，BusinessKey：{}", processInstanceId, businessKey);
    }

    @Override
    public void sendTaskCompletedMsg(String processInstanceId, String taskId, String businessKey) {
        BaseMap map = new BaseMap();
        map.put(FlowCloudConstant.FLOW_PROCESS_INSTANCE_ID, processInstanceId);
        map.put(FlowCloudConstant.FLOW_TASK_ID, taskId);
        map.put(FlowCloudConstant.FLOW_PROCESS_BUSINESS_KEY, businessKey);
        rabbitMqClient.sendMessage(FlowCloudConstant.MQ_FLOW_TASK_COMPLETED_EVENT, map);
        log.info("任务完成事件消息发送成功,流程实例ID：{}，任务ID：{},BusinessKey：{}", processInstanceId, taskId, businessKey);
    }
}
